﻿using System;
using System.Web.Script.Serialization;

namespace OF.Common
{
    public class Serializer
    {
        /// <summary>
        /// 将对象序列化成JOSN字符串
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string Serialize(Object model)
        {
            var javaScriptSerializer = new JavaScriptSerializer();
            string strJson = javaScriptSerializer.Serialize(model);
            return strJson;
        }
        /// <summary>
        /// 将JOSN字符串反序列化成一个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string json)
        {
            var javaScriptSerializer = new JavaScriptSerializer();
            var model = javaScriptSerializer.Deserialize<T>(json);
            return model;
        }
    }
}
